Passed
Push — master ( c5754d...1955b9 )
by Darío
07:59 queued 02:30
created

app.js ➔ ... ➔ $.ajax.success   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
$(function(){
2
3
    /**
4
     *  Submit forms with AJAX
5
     */
6
    $("body").delegate("form[data-role='ajax-request']", "submit", function(event)
7
    {
8
        event.preventDefault();
9
10
        var formObject = $(this);
11
12
        formObject.addClass('loading');
13
        formObject.find("input").attr("readonly", "readonly");
14
        formObject.find("select").attr("readonly", "readonly");
15
        formObject.find("button[type='submit']").attr("disabled", "disabled");
16
17
        var url  = $(this).attr('action');
18
        var type = $(this).attr('method');
19
        var box  = $(this).attr('data-response');
20
        var data = $(this).attr('data-object');
21
22
        var call = eval($(this).attr('data-callback')) || {};
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
23
24
        call.complete = call.complete || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
25
        call.success  = call.success || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
26
        call.before   = call.before  || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
27
        call.error    = call.error   || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
28
29
        var form_data = $(this).serializeArray();
30
31
        var parsed = eval(data);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
32
33
        for (var i in parsed)
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
34
        {
35
            form_data.push({ name: i, value: parsed[i] });
36
        }
37
38
        $.ajax({
39
            url: url,
40
            type: type,
41
            data: form_data,
42
            beforeSend: function() {
43
                var loader = "<div class='ui active inline loader'></div>";
44
                $(box).html(loader);
45
                call.before();
46
            },
47
            error: function(jqXHR, textStatus, errorThrown)
48
            {
49
                $(box).html("Error processing request!. " + errorThrown);
50
51
                var e = {};
52
                e.jqXHR = jqXHR;
53
                e.textStatus = textStatus;
54
                e.errorThrown = errorThrown;
55
56
                call.error(e);
57
            },
58
            success: function(data)
59
            {
60
                $(box).html(data);
61
                call.success(data);
62
            },
63
            complete: function(data)
64
            {
65
                formObject.find("input").removeAttr("readonly");
66
                formObject.find("select").removeAttr("readonly");
67
                formObject.find("button[type='submit']").removeAttr("disabled");
68
                formObject.removeClass('loading');
69
                call.success(data);
70
            }
71
        });
72
    });
73
74
    /**
75
     *  General AJAX request
76
     */
77
    $("body").delegate(":not(form)[data-role='ajax-request']", "click", function(event)
78
    {
79
        event.preventDefault();
80
81
        var url  = $(this).attr('data-url');
82
        var type = $(this).attr('data-type');
83
        var box  = $(this).attr('data-response');
84
        var data = $(this).attr('data-object');
85
        var frm  = $(this).attr('data-form');
86
87
        var call = eval($(this).attr('data-callback')) || {};
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
88
89
        call.complete = call.complete   || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
90
        call.success  = call.success || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
91
        call.before   = call.before  || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
92
        call.error    = call.error   || new Function();
0 ignored issues
show
Performance Best Practice introduced by
Using new Function() to create a function is slow and difficult to debug. Such functions do not create a closure. Consider using another way to define your function.
Loading history...
93
94
        var form_data = $(frm).serializeArray();
95
96
        var parsed = eval(data);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
97
98
        for (var i in parsed)
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
99
        {
100
            form_data.push({ name: i, value: parsed[i] });
101
        }
102
103
        $.ajax({
104
            url: url,
105
            type: type,
106
            data: form_data,
107
            beforeSend: function() {
108
                var loader = "<div class='ui active inline loader'></div>";
109
                $(box).html(loader);
110
                call.before();
111
            },
112
            error: function(jqXHR, textStatus, errorThrown)
113
            {
114
                $(box).html("Error processing request!. " + errorThrown);
115
116
                var e = {};
117
                e.jqXHR = jqXHR;
118
                e.textStatus = textStatus;
119
                e.errorThrown = errorThrown;
120
121
                call.error(e);
122
            },
123
            success: function(data)
124
            {
125
                $(box).html(data);
126
                call.success(data);
127
            },
128
            complete: function()
129
            {
130
                call.complete();
131
            }
132
        });
133
    });
134
});